package edu.ruc.cluster.action;

import java.io.IOException;

import edu.ruc.cluster.util.AbstractInteractiveCommand;

/**
 * This Class is used to chmod ~/.ssh and ~/.ssh/authorized_keys in slaves
 * Otherwise even with ssh master login still needs password
 * However, this command is rather simple. May be used with other command together
 * @author deke
 * 2012-12-07
 */
public class ChangeHadoopModCmd extends AbstractInteractiveCommand {

	public ChangeHadoopModCmd(String host, String passwd){
		super(host, passwd);
	}
	@Override
	public void interactProcess(String cmd) {
		// TODO Auto-generated method stub
		String[] commands = cmd.split("xxx");
		try{
			int count = 1;
			
			for(int i = 0 ; i < commands.length ; i ++){
				writor.write((commands[i]+ "\n").getBytes());
				writor.flush();
							
		    	char[] cbuf = new char[1024];			
				int ret = reader.read(cbuf);
				
				while(ret != -1){
					String info = new String(cbuf, 0, ret);
					System.out.print(info);
					if(info.contains("[" + getUser()+ "@" )){						
						if(count == 0){
							break;
						}
						count --;
					}
					ret = reader.read(cbuf);
				}
			}
		}catch(IOException e){
			logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
		}
	}
	/**
	 * About slaves
	 * @param hostt : usage of this class is to establish 'ssh hosttt' without password
	 * @param passwd  
	 */
	public void chmod(String dir){
		
		if(dir.charAt(dir.length() - 1) == '/')
			dir = dir.substring(0, dir.length() - 1);
		String cmd1 = "chmod +x " + dir + "/*";
		dealcmd(cmd1);
	}
	
	/*
	public static void main(String[] args){
		
			// setup ssh login without password from 'host' to 'hostssh'
		String host = "abc@dlnode27";
		String passwd = "123456";
		ChangeModCmd chmodcmd = new ChangeModCmd(host, passwd);
		chmodcmd.chmod();
	}
	*/
}
